using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Terminal.Advanced_AEUC_settings_controls.Settings_AUX;
using Terminal.Advanced_AEUC_settings_controls.Settings_Interfaces;
using Terminal.Advanced_AEUC_settings_controls.Settings_Editing_Forms;
using Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control;
using System.Drawing;

namespace Terminal.Advanced_AEUC_settings_controls.Setting_Advanced_Control
{
    /// <summary>
    /// An informational panel used to display the AEUC control settings values
    /// </summary>
    public class AEUCPanelInfoControl : Panel
    {
        bool NoGUI = false;
        AEUCTabbedParentControl cntrTrack;
        #region UI elements declaration
        Label lblNextControlLinkVal = new Label();
        Label lblLinkedSettingVal = new Label();
        Label lblCurChangeStateVal = new Label();
        Label lblSupportStateChangeVal = new Label();
        Label lblShadowedVal = new Label();
        Label lblAuxValueVal = new Label();
        Label lblEnabledVal = new Label();
        Label lblDefValueVal = new Label();
        Label lblMaxValueVal = new Label();
        Label lblMinValueVal = new Label();
        Label lblSizeVal = new Label();
        Label lblIDVal = new Label();
        Label lblNextControlLink = new Label();
        Label lblShadowed = new Label();
        Label lblAuxValue = new Label();
        Label lblCurChangeState = new Label();
        Label lblLinkedSetting = new Label();
        Label lblSupportStateChange = new Label();
        Label lblEnabled = new Label();
        Label lblMaxValue = new Label();
        Label lblMinValue = new Label();
        Label lblDefValue = new Label();
        Label lblSize = new Label();
        Label lblID = new Label();
        ListBox lbControlsPerPage = new ListBox();
        #endregion
        /// <summary>
        /// Flag indicating if the control was selected via a request from the info panel
        /// </summary>
        bool internal_change_request = false;

        public void RunningNoGUI(bool noGUI)
        {
            NoGUI = noGUI;
        }

        /// <summary>
        /// Public constructor linking the info panel to a tabbed parent control for AEUC setting controls
        /// </summary>
        /// <param name="icntrTrack">The tabbed parent which belonging AEUC controls will be monitored</param>
        public AEUCPanelInfoControl(AEUCTabbedParentControl icntrTrack)
        {
            cntrTrack = icntrTrack;

            #region Adding UI elements
            // 
            // lblNextControlLinkVal
            // 
            lblNextControlLinkVal.AutoSize = true;
            lblNextControlLinkVal.Location = new System.Drawing.Point(129, 246);
            lblNextControlLinkVal.Name = "lblNextControlLinkVal";
            lblNextControlLinkVal.Size = new System.Drawing.Size(53, 13);
            lblNextControlLinkVal.TabIndex = 29;
            lblNextControlLinkVal.Text = "Unknown";
            lblNextControlLinkVal.Visible = false;
            // 
            // lblLinkedSettingVal
            // 
            lblLinkedSettingVal.AutoSize = true;
            lblLinkedSettingVal.Location = new System.Drawing.Point(129, 233);
            lblLinkedSettingVal.Name = "lblLinkedSettingVal";
            lblLinkedSettingVal.Size = new System.Drawing.Size(53, 13);
            lblLinkedSettingVal.TabIndex = 28;
            lblLinkedSettingVal.Text = "Unknown";
            // 
            // lblCurChangeStateVal
            // 
            lblCurChangeStateVal.AutoSize = true;
            lblCurChangeStateVal.Location = new System.Drawing.Point(129, 220);
            lblCurChangeStateVal.Name = "lblCurChangeStateVal";
            lblCurChangeStateVal.Size = new System.Drawing.Size(53, 13);
            lblCurChangeStateVal.TabIndex = 27;
            lblCurChangeStateVal.Text = "Unknown";
            lblCurChangeStateVal.Visible = false;
            // 
            // lblSupportStateChangeVal
            // 
            lblSupportStateChangeVal.AutoSize = true;
            lblSupportStateChangeVal.Location = new System.Drawing.Point(129, 207);
            lblSupportStateChangeVal.Name = "lblSupportStateChangeVal";
            lblSupportStateChangeVal.Size = new System.Drawing.Size(53, 13);
            lblSupportStateChangeVal.TabIndex = 26;
            lblSupportStateChangeVal.Text = "Unknown";
            // 
            // lblShadowedVal
            // 
            lblShadowedVal.AutoSize = true;
            lblShadowedVal.Location = new System.Drawing.Point(215, 152);
            lblShadowedVal.Name = "lblShadowedVal";
            lblShadowedVal.Size = new System.Drawing.Size(53, 13);
            lblShadowedVal.TabIndex = 25;
            lblShadowedVal.Text = "Unknown";
            // 
            // lblAuxValueVal
            // 
            lblAuxValueVal.AutoSize = true;
            lblAuxValueVal.Location = new System.Drawing.Point(215, 138);
            lblAuxValueVal.Name = "lblAuxValueVal";
            lblAuxValueVal.Size = new System.Drawing.Size(53, 13);
            lblAuxValueVal.TabIndex = 24;
            lblAuxValueVal.Text = "Unknown";
            // 
            // lblEnabledVal
            // 
            lblEnabledVal.AutoSize = true;
            lblEnabledVal.Location = new System.Drawing.Point(215, 125);
            lblEnabledVal.Name = "lblEnabledVal";
            lblEnabledVal.Size = new System.Drawing.Size(53, 13);
            lblEnabledVal.TabIndex = 23;
            lblEnabledVal.Text = "Unknown";
            // 
            // lblDefValueVal
            // 
            lblDefValueVal.AutoSize = true;
            lblDefValueVal.Location = new System.Drawing.Point(215, 57);
            lblDefValueVal.Name = "lblDefValueVal";
            lblDefValueVal.Size = new System.Drawing.Size(53, 13);
            lblDefValueVal.TabIndex = 22;
            lblDefValueVal.Text = "Unknown";
            // 
            // lblMaxValueVal
            // 
            lblMaxValueVal.AutoSize = true;
            lblMaxValueVal.Location = new System.Drawing.Point(215, 44);
            lblMaxValueVal.Name = "lblMaxValueVal";
            lblMaxValueVal.Size = new System.Drawing.Size(53, 13);
            lblMaxValueVal.TabIndex = 21;
            lblMaxValueVal.Text = "Unknown";
            // 
            // lblMinValueVal
            // 
            lblMinValueVal.AutoSize = true;
            lblMinValueVal.Location = new System.Drawing.Point(215, 31);
            lblMinValueVal.Name = "lblMinValueVal";
            lblMinValueVal.Size = new System.Drawing.Size(53, 13);
            lblMinValueVal.TabIndex = 20;
            lblMinValueVal.Text = "Unknown";
            // 
            // lblSizeVal
            // 
            lblSizeVal.AutoSize = true;
            lblSizeVal.Location = new System.Drawing.Point(215, 18);
            lblSizeVal.Name = "lblSizeVal";
            lblSizeVal.Size = new System.Drawing.Size(53, 13);
            lblSizeVal.TabIndex = 19;
            lblSizeVal.Text = "Unknown";
            // 
            // lblIDVal
            // 
            lblIDVal.AutoSize = true;
            lblIDVal.Location = new System.Drawing.Point(215, 5);
            lblIDVal.Name = "lblIDVal";
            lblIDVal.Size = new System.Drawing.Size(53, 13);
            lblIDVal.TabIndex = 18;
            lblIDVal.Text = "Unknown";
            // 
            // lblNextControlLink
            // 
            lblNextControlLink.AutoSize = true;
            lblNextControlLink.Location = new System.Drawing.Point(3, 246);
            lblNextControlLink.Name = "lblNextControlLink";
            lblNextControlLink.Size = new System.Drawing.Size(124, 13);
            lblNextControlLink.TabIndex = 17;
            lblNextControlLink.Text = "Next control in link group";
            lblNextControlLink.Visible = false;
            // 
            // lblShadowed
            // 
            lblShadowed.AutoSize = true;
            lblShadowed.Location = new System.Drawing.Point(129, 152);
            lblShadowed.Name = "lblShadowed";
            lblShadowed.Size = new System.Drawing.Size(78, 13);
            lblShadowed.TabIndex = 16;
            lblShadowed.Text = "Value recovery";
            // 
            // lblAuxValue
            // 
            lblAuxValue.AutoSize = true;
            lblAuxValue.Location = new System.Drawing.Point(129, 138);
            lblAuxValue.Name = "lblAuxValue";
            lblAuxValue.Size = new System.Drawing.Size(67, 13);
            lblAuxValue.TabIndex = 15;
            lblAuxValue.Text = "Aux use only";
            // 
            // lblCurChangeState
            // 
            lblCurChangeState.AutoSize = true;
            lblCurChangeState.Location = new System.Drawing.Point(3, 220);
            lblCurChangeState.Name = "lblCurChangeState";
            lblCurChangeState.Size = new System.Drawing.Size(67, 13);
            lblCurChangeState.TabIndex = 14;
            lblCurChangeState.Text = "Current state";
            lblCurChangeState.Visible = false;
            // 
            // lblLinkedSetting
            // 
            lblLinkedSetting.AutoSize = true;
            lblLinkedSetting.Location = new System.Drawing.Point(3, 233);
            lblLinkedSetting.Name = "lblLinkedSetting";
            lblLinkedSetting.Size = new System.Drawing.Size(73, 13);
            lblLinkedSetting.TabIndex = 13;
            lblLinkedSetting.Text = "Linked setting";
            // 
            // lblSupportStateChange
            // 
            lblSupportStateChange.AutoSize = true;
            lblSupportStateChange.Location = new System.Drawing.Point(3, 207);
            lblSupportStateChange.Name = "lblSupportStateChange";
            lblSupportStateChange.Size = new System.Drawing.Size(112, 13);
            lblSupportStateChange.TabIndex = 12;
            lblSupportStateChange.Text = "State change tracking";
            // 
            // lblEnabled
            // 
            lblEnabled.AutoSize = true;
            lblEnabled.Location = new System.Drawing.Point(129, 125);
            lblEnabled.Name = "lblEnabled";
            lblEnabled.Size = new System.Drawing.Size(46, 13);
            lblEnabled.TabIndex = 11;
            lblEnabled.Text = "Enabled";
            // 
            // lblMaxValue
            // 
            lblMaxValue.AutoSize = true;
            lblMaxValue.Location = new System.Drawing.Point(129, 44);
            lblMaxValue.Name = "lblMaxValue";
            lblMaxValue.Size = new System.Drawing.Size(80, 13);
            lblMaxValue.TabIndex = 10;
            lblMaxValue.Text = "Maximum value";
            // 
            // lblMinValue
            // 
            lblMinValue.AutoSize = true;
            lblMinValue.Location = new System.Drawing.Point(129, 31);
            lblMinValue.Name = "lblMinValue";
            lblMinValue.Size = new System.Drawing.Size(77, 13);
            lblMinValue.TabIndex = 9;
            lblMinValue.Text = "Minimum value";
            // 
            // lblDefValue
            // 
            lblDefValue.AutoSize = true;
            lblDefValue.Location = new System.Drawing.Point(129, 57);
            lblDefValue.Name = "lblDefValue";
            lblDefValue.Size = new System.Drawing.Size(70, 13);
            lblDefValue.TabIndex = 8;
            lblDefValue.Text = "Default value";
            // 
            // lblSize
            // 
            lblSize.AutoSize = true;
            lblSize.Location = new System.Drawing.Point(129, 18);
            lblSize.Name = "lblSize";
            lblSize.Size = new System.Drawing.Size(27, 13);
            lblSize.TabIndex = 7;
            lblSize.Text = "Size";
            // 
            // lblID
            // 
            lblID.AutoSize = true;
            lblID.Location = new System.Drawing.Point(129, 5);
            lblID.Name = "lblID";
            lblID.Size = new System.Drawing.Size(18, 13);
            lblID.TabIndex = 6;
            lblID.Text = "ID";
            // 
            // lbControlsPerPage
            // 
            lbControlsPerPage.FormattingEnabled = true;
            lbControlsPerPage.Location = new System.Drawing.Point(3, 5);
            lbControlsPerPage.Name = "lbControlsPerPage";
            lbControlsPerPage.Size = new System.Drawing.Size(120, 199);
            lbControlsPerPage.TabIndex = 5;
            //
            // Adding all controls to the panel
            //
            Controls.Add(lblNextControlLinkVal);
            Controls.Add(lblLinkedSettingVal);
            Controls.Add(lblCurChangeStateVal);
            Controls.Add(lblSupportStateChangeVal);
            Controls.Add(lblShadowedVal);
            Controls.Add(lblAuxValueVal);
            Controls.Add(lblEnabledVal);
            Controls.Add(lblDefValueVal);
            Controls.Add(lblMaxValueVal);
            Controls.Add(lblMinValueVal);
            Controls.Add(lblSizeVal);
            Controls.Add(lblIDVal);
            Controls.Add(lblNextControlLink);
            Controls.Add(lblShadowed);
            Controls.Add(lblAuxValue);
            Controls.Add(lblCurChangeState);
            Controls.Add(lblLinkedSetting);
            Controls.Add(lblSupportStateChange);
            Controls.Add(lblEnabled);
            Controls.Add(lblMaxValue);
            Controls.Add(lblMinValue);
            Controls.Add(lblDefValue);
            Controls.Add(lblSize);
            Controls.Add(lblID);
            Controls.Add(lbControlsPerPage);
            #endregion

            Size = new Size(280, 270);

            lbControlsPerPage.DisplayMember = "AEUC_DisplayName";
            lbControlsPerPage.SelectedIndexChanged +=new EventHandler(lbControlsPerPage_SelectedIndexChanged);

            cntrTrack.SelectedIndexChanged +=new EventHandler(cntrTrack_SelectedIndexChanged);
            cntrTrack.Deselecting += new TabControlCancelEventHandler(cntrTrack_Deselecting);
            cntrTrack.OnAEUCControlsUpdated +=new AEUCTabbedParentControl.AEUCControlsUpdated(cntrTrack_OnAEUCControlsUpdated);

            BorderStyle = BorderStyle.Fixed3D;
        }

        void cntrTrack_OnAEUCControlsUpdated()
        {
            if (!NoGUI)
            {
                lbControlsPerPage.Items.Clear();
                if (cntrTrack.SelectedTab != null)
                {
                    for (int i = 0; i < cntrTrack.SelectedTab.Controls.Count; i++)
                    {
                        if (cntrTrack.SelectedTab.Controls[i] is IAEUCSetting)
                        {
                            lbControlsPerPage.Items.Add(cntrTrack.SelectedTab.Controls[i]);
                            cntrTrack.SelectedTab.Controls[i].GotFocus += new EventHandler(ctrlAquireFocus);
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Updates the information in the panel when the selected item is changed
        /// </summary>
        /// <param name="sender">Event initiator</param>
        /// <param name="e">Event arguments</param>
        void lbControlsPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            IAEUCSetting tmpSetting = (IAEUCSetting)lbControlsPerPage.SelectedItem;
            if (tmpSetting != null)
            {
                lblIDVal.Text = tmpSetting.AEUC_Id.ToString();
                lblSizeVal.Text = tmpSetting.AEUC_Size.ToString();
                lblMinValueVal.Text = tmpSetting.AEUC_Min.ToString();
                lblMaxValueVal.Text = tmpSetting.AEUC_Max.ToString();
                lblDefValueVal.Text = tmpSetting.AEUC_Default.ToString();

                lblEnabledVal.Text = tmpSetting.AEUC_Enabled.ToString();
                lblAuxValueVal.Text = tmpSetting.AEUC_AUXSetting.ToString();
                lblShadowedVal.Text = tmpSetting.AEUC_ShadowedValue.ToString();

                lblSupportStateChangeVal.Text = tmpSetting.AEUC_SupportStateSense.ToString();
                lblCurChangeState.Visible = lblCurChangeStateVal.Visible = tmpSetting.AEUC_SupportStateSense;
                //lblCurChangeStateVal.Text;


                lblLinkedSettingVal.Text = tmpSetting.AEUC_LinkedSetting.ToString();
                lblNextControlLink.Visible = lblNextControlLinkVal.Visible = tmpSetting.AEUC_LinkedSetting;
                if (tmpSetting.AEUC_NextSettingLink != null)
                    lblNextControlLinkVal.Text = tmpSetting.AEUC_NextSettingLink.ToString();

                internal_change_request = true;
                ((Control)lbControlsPerPage.SelectedItem).Focus();
                internal_change_request = false;
            }
        }

        /// <summary>
        /// When a tab page is being deselected, any event relations to its controls are dismissed
        /// </summary>
        /// <param name="sender">Event initiator</param>
        /// <param name="e">Event argumetns</param>
        void cntrTrack_Deselecting(object sender, TabControlCancelEventArgs e)
        {
            for (int i = 0; i < lbControlsPerPage.Items.Count; i++)
            {
                ((Control)lbControlsPerPage.Items[i]).GotFocus -= new EventHandler(ctrlAquireFocus);
            }
        }

        /// <summary>
        /// When the tracked AEUCTabbedParentControl changes tab pages, the controls belonging to the new tab page are loaded
        /// </summary>
        /// <param name="sender">Event initiator</param>
        /// <param name="e">Event arguments</param>
        void cntrTrack_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbControlsPerPage.Items.Clear();
            #region Reseting info labels to default state
            lblIDVal.Text = "Unknown";
            lblSizeVal.Text = "Unknown";
            lblMinValueVal.Text = "Unknown";
            lblMaxValueVal.Text = "Unknown";
            lblDefValueVal.Text = "Unknown";
            lblEnabledVal.Text = "Unknown";
            lblAuxValueVal.Text = "Unknown";
            lblShadowedVal.Text = "Unknown";
            lblSupportStateChangeVal.Text = "Unknown";
            lblCurChangeState.Visible = lblCurChangeStateVal.Visible = false;
            lblCurChangeStateVal.Text = "Unknown";
            lblLinkedSettingVal.Text = "Unknown";
            lblNextControlLink.Visible = lblNextControlLinkVal.Visible = false;
            lblNextControlLinkVal.Text ="Unknown";
            #endregion

            if (cntrTrack.SelectedTab != null)
            {
                for (int i = 0; i < cntrTrack.SelectedTab.Controls.Count; i++)
                {
                    if (cntrTrack.SelectedTab.Controls[i] is IAEUCSetting)
                    {
                        lbControlsPerPage.Items.Add(cntrTrack.SelectedTab.Controls[i]);
                        cntrTrack.SelectedTab.Controls[i].GotFocus += new EventHandler(ctrlAquireFocus);
                    }
                }
            }
        }

        /// <summary>
        /// When a control belonging to the tracked tab page gets input focus, the info panel updates its contents
        /// </summary>
        /// <param name="sender">Event initiator</param>
        /// <param name="e">Event arguments</param>
        void ctrlAquireFocus(object sender, EventArgs e)
        {
            if (!internal_change_request)
                lbControlsPerPage.SelectedItem = sender;
        }
    }
}
